package faceService;

import com.qcloud.image.ImageClient;
import com.qcloud.image.common_utils.CommonFileUtils;
import com.qcloud.image.request.FaceAddFaceRequest;
import util.AppContast;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import static com.qcloud.image.http.RequestBodyKey.BUCKET;
import static com.qcloud.image.http.RequestBodyKey.SEQ;

/**
 * Created by Administrator on 2017/9/28.
 */
public class AddFace {
    public void addFace(String personId, String filePath) throws Exception {
        ImageClient imageClient = new ImageClient(AppContast.APP_ID, AppContast.SERCET_ID, AppContast.SERCET_KEY);
        String bucketName = BUCKET;

        String addfacePersonId = personId;
        String addfacePersonTag = "test";

        List<String> fileNameLists = new ArrayList<String>();
        List<String> filePathLists = new ArrayList<String>();
        String path = filePath;
        File file = new File(path);
        if (file.exists()) {
            File[] files = file.listFiles();
            for (File file1 : files) {
                if (!file1.isDirectory()) {
                    try {
                        fileNameLists.add(file1.getName());
                        filePathLists.add(file1.getAbsolutePath());
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        }

        String[] addFaceNameList = new String[5];
        String[] addFaceImageList = new String[5];
        for (int i = 0; i < 5; i++) {
            addFaceImageList[i] = CommonFileUtils.getFileContent(filePathLists.get(i));
            addFaceNameList[i] = fileNameLists.get(i);
        }

        FaceAddFaceRequest addFaceReq = new FaceAddFaceRequest(bucketName, addFaceNameList, addFaceImageList, addfacePersonId, addfacePersonTag);
        String ret = imageClient.faceAddFace(addFaceReq);
        System.out.println("add face ret:" + ret);
    }

    public void addFaceImg(String personId, String imgPath, String name) throws Exception {
        ImageClient imageClient = new ImageClient(AppContast.APP_ID, AppContast.SERCET_ID, AppContast.SERCET_KEY);
        String bucketName = BUCKET;

        String addfacePersonId = personId;
        String addfacePersonTag = "test";

        String[] imgPathList = new String[1];
        imgPathList[0] = CommonFileUtils.getFileContent(imgPath);
        String[] nameList = new String[1];
        nameList[0] = name;

        FaceAddFaceRequest addFaceReq = new FaceAddFaceRequest(bucketName, nameList, imgPathList, addfacePersonId, addfacePersonTag);
        String ret = imageClient.faceAddFace(addFaceReq);
        System.out.println("add face ret:" + ret);
    }
}
